﻿using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace LaunchPad.Core
{
    public class ProductInfo : INotifyPropertyChanged, IEquatable<ProductInfo>
    {
        private int _total;
        private Product _product;
        private int _inShow;

        public ProductInfo()
        {
        }

        public ProductInfo(Product product)
        {
            _product = product;
        }

        [XmlIgnore]
        public Product Product
        {
            get { return _product; }
            set
            {
                if (_product != value)
                {
                    _product = value;
                    OnPropertyChanged("Product");
                }
            }
        }

        [XmlAttribute]
        public int Total
        {
            get { return _total; }
            set
            {
                if (_total != value)
                {
                    _total = value;
                    OnPropertyChanged("Total");
                    OnPropertyChanged("InStock");
                }
            }
        }

        [XmlIgnore]
        public int InShow
        {
            get { return _inShow; }
            set
            {
                if (_inShow != value)
                {
                    _inShow = value;
                    OnPropertyChanged("InShow");
                    OnPropertyChanged("InStock");
                }
            }
        }

        [XmlIgnore]
        public int InStock
        {
            get { return Total - InShow; }
            set { Total = InShow + value; }
        }

        [XmlElement("ProductRef")]
        public IdRef<Product> ProductReference
        {
            get
            {
                if (Product == null)
                    return null;
                return new IdRef<Product>(Product);
            }

            set
            {
                if (value != null)
                {
                    Product product;

                    if (ProductFactory.Instance.TryGetProduct(value, out product))
                    {
                        Product = product;
                        OnPropertyChanged("ProductReference");
                    }
                }
            }
        }

        #region IEquatable<ProductInfo> Members

        public bool Equals(ProductInfo other)
        {
            return Product == other.Product;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        public override bool Equals(object obj)
        {
            if (obj is ProductInfo)
            {
                return Product == (obj as ProductInfo).Product;
            }
            return false;
        }

        public override int GetHashCode()
        {
            if (Product == null)
                return base.GetHashCode();
            return Product.GetHashCode();
        }
    }
}